package com.xiaoyg.lock;

public class TestVisibilitySync {
    /**
     * volatile(缓存一致性，强制失效工作内存)
     * 执行sync 逻辑的时候，会执行lock原子操作，会使共工作内存失效
     */
    private static /*volatile*/ boolean flag = true;
    private static Object obj = new Object();
    public static void main(String[] args) throws InterruptedException {

        new Thread(()->{
            while(flag){
                synchronized (obj){

                }
            }
        }).start();

        Thread.sleep(2000);

        new Thread(()->{
            flag = false;
            System.out.printf("线程修改了变量的值为false");
        }).start();
    }

}
